package com.cheer.controller;

import com.cheer.common.Result;
import com.cheer.entity.OaUserEntity;
import com.cheer.enums.OaArticleClassEnum;
import com.cheer.job.OaLearnListener;
import com.cheer.service.*;
import com.cheer.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author cheer
 */
@Api(tags = "首页")
@RestController
@RequestMapping("/main")
public class OaHomeController {

    @Autowired
    private OaBannerService oaBannerService;

    @Autowired
    private OaNoticeService oaNoticeService;

    @Autowired
    private OaArticleClassService oaArticleClassService;

    @Autowired
    private OaArticleService oaArticleService;

    @Autowired
    private OaIntegralService oaIntegralService;

    @Autowired
    private AuthService authService;

    @Autowired
    private FlowerService flowerService;

    @Autowired
    private TaskService taskService;


    @GetMapping
    @ApiOperation("首页改造 TODO")
    public Result<OaHomeVO> home(HttpSession session) {

        String openid = authService.getOpenid(session);
        OaLearnListener.removeTask(openid);
        UserMainVO userMainVo = oaIntegralService.getUserMainVo(authService.getUserId(session),
                authService.getOpenid(session));
        userMainVo.setAbleGetFlowerNum(flowerService.getFlower(authService.getUserId(session)));
        List<OaArticleSimpleVO> oaArticleSimpleVOS = oaArticleService.listRecommend(1, 10);
        oaArticleSimpleVOS.forEach(oaArticleSimpleVO -> oaArticleSimpleVO.setIntegral(4));

        return Result.success(new OaHomeVO()
                .setBanner(oaBannerService.getFirst())
                .setNotice(oaNoticeService.getFirst())
                .setArticleList(oaArticleClassService.listByLocation(OaArticleClassEnum.MAIN.getCode()))
                .setRecommendList(oaArticleSimpleVOS)
                .setOaTaskInfoVO(taskService.getTaskStatus(openid, authService.getExUserByOpenId(session)))
                .setUserMainVO(userMainVo)
        );
    }

    @GetMapping("/credit")
    public Result<List<CreditDateVO>> credit(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                             @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                             HttpSession session) {
        return Result.success(oaIntegralService.listCredit(pageNum, pageSize, authService.getUserId(session)));
    }

    @GetMapping("/task")
    @ApiOperation("今日任务")
    public Result<OaTaskInfoVO> task(HttpSession session) {
        OaTaskInfoVO oaTaskInfoVO = taskService.task(authService.getOpenid(session),
                authService.getExUserByOpenId(session));
        return Result.success(oaTaskInfoVO);
    }
}
